'use strict';
import React from 'react';
import {
    View,
    ListView,
    RefreshControl,
} from 'react-native';
import ToolBar from '../Components/ToolBar';
import EventDetailsPage from "./EventDetailsPage";
import RuralActivity from "../Components/RuralActivity";
import RuralLibraryAPIService from "../Commom/RuralLibraryAPIService";
import Global from '../Commom/GlobalConst';
import Toast from '../Utils/toast';
const width = Global.Const.size.width;
/**
 *  全部活动
 */
class AllRuralActivityPage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            dataSource: new ListView.DataSource({
                rowHasChanged: (r1, r2) => r1 !== r2,
            }),
            isRefreshing: false,
            record_count: 1,
            page: 1,
            activityDataList: [],
        };
    }

    goToEventDetails(id) {
        this.props.navigator.push({
            name: 'EventDetailsPage',
            component: EventDetailsPage,
            passProps: {
                ID: id
            }
        });
    }

    componentDidMount() {
        RuralLibraryAPIService.getActivityList(this.props.ID).then((ret)=> {
            this.setState({
                dataSource: this.state.dataSource.cloneWithRows(ret.result),
                activityDataList: ret.result,
                record_count: ret.record_count,
                page: Number(ret.page),
            });
        }).catch((err)=> {

        });
    }

    //刷新数据
    onRefreshData() {
        this.setState({
            isRefreshing: true,
        });
        RuralLibraryAPIService.getActivityList(this.props.ID).then((ret)=> {
            this.setState({
                dataSource: this.state.dataSource.cloneWithRows(ret.result),
                activityDataList: ret.result,
                isRefreshing: false,
                record_count: ret.record_count,
                page: Number(ret.page),
            });
            Toast.show('刷新成功');
        }).catch((err)=> {
            this.setState({
                isRefreshing: false,
            });
        });
    }

    //加载更多数据
    loadMoreData() {
        console.log('why');
        if (this.state.page < this.state.record_count) {
            this.setState({
                isRefreshing: true,
            });
            RuralLibraryAPIService.getActivityList(this.props.ID).then((ret)=> {
                let wanjian = [];
                wanjian = this.state.activityDataList;
                for (var i = 0; i < ret.result.length; i++) {
                    wanjian.push(ret.result[i])
                }
                this.setState({
                    dataSource: this.state.dataSource.cloneWithRows(wanjian),
                    activityDataList: wanjian,
                    isRefreshing: false,
                    record_count: ret.record_count,
                    page: Number(ret.page),
                });
                Toast.show('加载成功');
            }).catch((err)=> {
            });
        } else {
            if (this.state.page != 1) {
                Toast.show('木有更多数据');
            }
        }

    }

    _renderRow(rowData) {
        return (
            <View style={{height:128,width}}>
                <RuralActivity activity={rowData}
                               onItemClick={() => this.goToEventDetails(rowData.id)}/>
                <View style={{height:8,width,backgroundColor:Global.Const.transparent}}></View>
            </View>
        );


    }

    render() {
        return (
            <View style={{flex: 1,backgroundColor:'white'}}>
                <ToolBar leftFunc={()=> this.props.navigator.pop()} title="全部活动" rightFunc={()=> {
                }} rightImage={0}/>
                <ListView
                    refreshControl={
                                          <RefreshControl
                                            refreshing={this.state.isRefreshing}
                                            onRefresh={() => {this.onRefreshData()}}
                                            colors={['#4CAF50', '#4CAF50', '#4CAF50', '#4CAF50']}
                                            progressBackgroundColor="white"
                                          /> }
                    onEndReachedThreshold={100}
                    initialListSize={1}
                    onEndReached={() => {this.loadMoreData()}}
                    dataSource={this.state.dataSource}
                    style={{borderTopWidth:2,borderColor:Global.Const.transparent}}
                    enableEmptySections={true}
                    renderRow={(rowData) => this._renderRow(rowData)}
                />

            </View>
        );
    }
}
export default AllRuralActivityPage;